package com.bobo.service.photo;

import com.bobo.dao.photo.IPhotoDao;
import com.bobo.dao.photo.PhotoDaoImpl;
import com.bobo.dto.Page;
import com.bobo.model.Photo;

import java.util.List;

/**
 * Created by bobo on 2016/10/17.
 */
public class PhotoServiceImpl implements IPhotoService {


    private IPhotoDao photoDao  = new PhotoDaoImpl();

    @Override
    public Integer create(Photo photo) {

        return photoDao.create(photo);
    }

    @Override
    public boolean update(Photo photo) {

        return photoDao.update(photo);
    }

    @Override
    public Photo getPhoto(Integer id) {
        return photoDao.getById(id);
    }

    @Override
    public boolean delete(Integer id) {
        return photoDao.delete(id);
    }

    @Override
    public List<Photo>  clearPhoto() {
        return photoDao.clearPhoto();
    }

    @Override
    public List<Photo> getList(Integer status) {
        return photoDao.getPhotos(0, 0, status);
    }

    @Override
    public Page<Photo> getPage(int current, int pageSize) {
        Page<Photo> photoPage = new Page<>();

        int count = photoDao.getCount(1, 5);
        if(pageSize < 1) {
            pageSize = 10;
        }
        int countPage = count % pageSize != 0 ? (count / pageSize + 1) : (count / pageSize);


        if(current < 1 ) current = 1;
        if(current > countPage)   current  = countPage;

        photoPage.setList(photoDao.getPhotos(current,pageSize, 1, 5));
        photoPage.setPageNum(countPage);
        photoPage.setCurrent(current);
        photoPage.setPageSize(pageSize);

        return photoPage;
    }

    @Override
    public Page<Photo> getCp(int current, int pageSize) {
        Page<Photo> photoPage = new Page<>();

        int count = photoDao.getCount(-1);
        if(pageSize < 1) {
            pageSize = 10;
        }
        int countPage = count % pageSize != 0 ? (count / pageSize + 1) : (count / pageSize);


        if(current < 1 ) current = 1;
        if(current > countPage)   current  = countPage;

        photoPage.setList(photoDao.getPhotos(current,pageSize,-1));
        photoPage.setPageNum(countPage);
        photoPage.setCurrent(current);
        photoPage.setPageSize(pageSize);

        return photoPage;
    }
}
